﻿#Include ..\baseHandle.ahk
#Include ..\..\core\handleMgr.ahk

class Exec extends BaseHandle {

  static Handle(parsed) {
    if (r := Mgr.Check(parsed.target)).valid {
      if r.handler.Prototype.__Class = 'Exec'
        return this.Fail('Recursive calls(exec)')
      echo := Mgr.Call(r.handler, r.parsed)
      if echo.extra = MeowTool.asyncFlag {
        res := ''
        echo.r.done := (_, _r, flag) => res := { flag: flag, r: _r }
        echo.r.succ := (_, _r) => res := { flag: true, r: _r }
        echo.r.fail := (_, _r) => res := { flag: false, r: _r }
        echo.r.Call()
        return this.Done(res.r, res.flag)
      } else return this.Succ(echo.r)
    }
    return this.Fail(r.msg)
  }

  static Spec() {
    return {
      cmd: 'exec',
      desc: '执行一条注册命令，不可调用自己',
      format: 'exec <target>',
      args: {
        desc: '一条可执行命令',
        isOptional: false,
        cacheable: false,
      },
      extra: {
        isEnable: false
      }
    }
  }
}
